<template>
  <!-- tab基本信息表单  -->
  <el-scrollbar style="height: 100%;margin-top:10px;">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="90px" class="form-input-m" inline-message>
      <el-form-item label="内容" prop="descText">
        <el-input v-model="formData.descText" placeholder="请输入用途" type="textarea" rows='8' clearable></el-input>
      </el-form-item>
      <el-form-item prop="isEnable" label="是否启用">
        <vxe-switch v-model="formData.isEnable" size="mini" open-label="是" close-label="否" :open-value="1" :close-value="0"></vxe-switch>
      </el-form-item>
      <el-form-item prop="order" label="排序">
        <el-input-number v-model="formData.seq" :min="1" :max="1000" />
      </el-form-item>
      <el-form-item label="" prop="">
        <el-button type="primary" @click="saveForm">保存</el-button>
      </el-form-item>
    </el-form>
  </el-scrollbar>
</template>

<script lang="ts" setup>
// 引入组件区 ==========
import {
  ref,
  reactive,
  watch,
  getCurrentInstance,
  PropType,
  onMounted
} from 'vue';
import { IsEmpty, formatDateTime } from '@/utils/common'; //导入工具函数
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { AppStore } from '@/stores/store';
import { FormInstance } from 'element-plus';
import request from '@/api';

// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
let store = AppStore();
let orgId = ref(null);
const formRef = ref(null); //表单ref标记
// 表单绑定的数据类型限制
interface FormDataVO {
  id: string /*主键*/;
  descCode: String;/*编码 */
  descType: String;/*内容类型 */
  descName: String;/*内容名称 */
  descText: String;/*内容 */
  isEnable: Number;/*是否启用 */
  seq: Number;/*排序 */
}
// 表单绑定的数据
let formData = reactive<FormDataVO>({
  id: '',
  descCode: '',/*编码 */
  descType: '',/*内容类型 */
  descName: '',/*内容名称 */
  descText: '',/*内容 */
  isEnable: 1,/*是否启用 */
  seq: 1,/*是否启用 */
});
// 表单校验规则
const formRules: any = {
  descText: [{ required: true, message: '请输入内容' }]
};

defineExpose({
  addNew,
  saveForm
});

// 父组件传值
let props = defineProps({
  currentRow: { type: Object as PropType<any>, required: true },
  descType: { type: String as PropType<any>, required: true }
}) as any;
watch(
  () => props.currentRow,
  (nval, oval) => {
    Object.assign(formData, nval);
  },
  {
    deep: true,
    immediate: true
  }
);


onMounted(() => {
  orgId = store.userInfo.orgId;
  //formData.bankNumber=generateCode();
});


// 方法定义区 =============
//生成题库编码
function generateCode() {
  let now = new Date();
  console.log(now, "now");
  let y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate(),
    h = now.getHours(),
    mm = now.getMinutes(),
    s = now.getSeconds(),
    ms = now.getMilliseconds();
  return "TM" + y + m + d + h + mm + s + ms;
}
function addNew(callback: any) {
  clear();
  if (callback) {
    callback({ a: '初始化完成' });
  }
}
// 清空表单
function clear() {
  formData.id = "";
  formData.descCode = "";/*编码 */
  formData.descType = props.descType;/*内容类型 */
  formData.descName = "";/*内容名称 */
  formData.descText = "";/*内容 */
  formData.isEnable = 1;/*是否启用 */
  formData.seq = 1;/*排序 */
}

// 保存信息
function saveForm() {
  submitForm();
}

// 保存
const submitForm = () => {
  //let elInfo = proxy.$refs.formRef;
  formRef.value?.validate(async (valid: any) => {
    if (!valid) {
      return false;
    }
    let res: any;
    if (IsEmpty(formData.id)) {
      formData.descType = props.descType;/*内容类型 */
      res = await request({
        url: '/bac/diagnosTempDesc/addDiagnosTempDesc',
        method: 'post',
        data: formData
      });
    } else {
      res = await request({
        url: '/bac/diagnosTempDesc/editDiagnosTempDesc',
        method: 'post',
        data: formData
      });
    }
    res.status.code === 1 ? VxeMessage(res.status.msg, VxeNotifyType.success) : VxeMessage(res.status.msg, VxeNotifyType.error);
    clear();
    proxy.$emit('findList');
  });
};
</script>

<style lang="scss" scoped>
// 中等尺寸时 配置扩展属性按钮
.el-button--default {
  padding: 5px 11px;
}
</style>

